<template>
  <el-dialog
    title="新增权限"
    :visible.sync="dialogShow"
    @close="HandlerClose"
  >
    <el-form ref="form" :model="formData" :rules="rules" label-width="80px" :inline="false" size="normal">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="formData.code" />
      </el-form-item>
      <el-form-item label="权限描述">
        <el-input v-model="formData.description" />
      </el-form-item>
      <el-form-item label="开启">
        <el-switch v-model="formData.enVisible" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitFn">提交</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>

</template>

<script>
import { addPermission, updatePermission } from '@/api/permisson.js'
export default {
  data() {
    return {
      dialogShow: false, // 控制弹窗
      rules: { // 校验规则
        name: [{ required: true, message: '请输入权限名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入权限名称', trigger: 'blur' }]
      },
      formData: { // 表单数据
        enVisible: '',
        name: '',
        code: '',
        description: '',
        type: '',
        pid: ''
      }
    }
  },
  methods: {
    async  submitFn() {
      await this.$refs.form.validate()
      if (this.formData.id) {
        // 编辑
        await updatePermission(this.formData)
      } else {
        // 新增
        await addPermission(this.formData)
      }
      this.$message.success('操作成功')
      this.$emit('update')
      this.dialogShow = false
    },
    // 关闭弹框之前重置表单数据
    HandlerClose() {
      this.formData = {
        enVisible: '',
        name: '',
        code: '',
        description: '',
        type: '',
        pid: '' }// 重置表单数据
      this.$refs.form.clearValidate()
    }
  }

}
</script>

<style>

</style>
